package com.qf.consumer;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class HelloConsumer {

    @Value("${time.cost}")
    private Integer timeCost;

//    @RabbitListener(queues = "helloworld")
//    public void getMsg(Message message){
//        byte[] body = message.getBody();
//        System.out.println("接收到消息为:"+new String(body));
//    }

    @RabbitListener(queues = "helloworld")
    public void getMsg(String msg, Message message, Channel channel) throws InterruptedException, IOException {
//        byte[] body = message.getBody();
        System.out.println("接收到消息为:"+msg);
        Thread.sleep(timeCost);

        // 手动确认消息
        // 参数1：要确认的消息的编号
        // 参数2：是不是批量确认消息 （prefetch=1），非批量
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);


    }

}
